import { baseTableList, appExists, appDelete, appAdd, appUpdate, appStart, appStop } from '../scripts/api'
const validatorAppName = (rule, value, callback) => {
  if (!value) {
    callback(new Error('应用名称不能为空(前端验证)'))
  } else {
    appExists(value).then(response => {
      if (response.data) {
        callback(new Error('应用名称已经存在(后端验证)'))
      } else {
        callback()
      }
    }).catch(e => {
      callback(new Error('应用名称验证失败'))
    })
  }
}
export default {
  name: 'baseTable',
  data () {
    return {
      selectedItems: [],
      modalShow: false,
      modalFlag: '',
      deleteId: '',
      modalForm: {
        id: '',
        appName: '',
        appType: 'APP',
        appDesc: ''
      },
      ruleModalForm: {
        appName: [
          { required: true, validator: validatorAppName, trigger: 'blur' }
        ]
      },
      appTypeList: [
        {
          code: 'APP', text: 'APP'
        },
        {
          code: 'DNS', text: 'DNS'
        },
        {
          code: 'DB', text: 'DB'
        }
      ],
      baseTableData: {
        showSearch: true,
        tData: (pageParam) => {
          // Object.assign(pageParam)
          return baseTableList({
            params: pageParam
          })
          // .then(data=>{
          //   console.info('data',data)
          // })
        },
        tColumns: [
          {
            type: 'selection',
            width: 60,
            align: 'center'
          },
          {
            type: 'expand',
            width: 30,
            extendFormat: 'expand',
            expand: (params) => {
              if (params) {
                const list = [
                  { title: '标题一', key: 'appDesc' }
                ]
                return { list: list, columns: 1 }
              }
            }
          },
          {
            title: '标题一',
            key: 'appType',
            width: 100,
            align: 'center',
            sortable: true
          },
          {
            title: '标题二',
            key: 'appName',
            minWidth: 200,
            align: 'center',
            sortable: true
          },
          {
            title: '标题三',
            key: 'appProcess',
            width: 150,
            align: 'center',
            sortable: true,
            slot: 'progress'
          },
          {
            title: '创建时间',
            key: 'date',
            maxWidth: 150,
            align: 'center',
            extendFormat: 'date'
          },
          {
            title: '操作',
            maxWidth: 150,
            align: 'center',
            slot: 'action'
          }
        ]

      }
    }
  },
  methods: {
    selectItem (selection) {
      this.selectedItems = selection
    },
    handBachOperate () {
      if (this.selectedItems.length === 0 || this.selectedItems === undefined) {
        this.$Message.warning(this.$t('common.needSelectedFirst'))
        return false
      }
      return true
      // this.deleteModal = true
    },
    // 添加
    handBtnAdd () {
      this.modalShow = true
      this.modalFlag = 'update'
    },
    // 修改
    beforeBtnUpdate (row) {
      this.modalShow = true
      this.modalFlag = 'update'
      const { id, appName, appType, appDesc } = row
      Object.assign(this.modalForm, { id, appName, appType, appDesc })
    },

    btnDelete (Proxy, id) {
      const ids = id || this.selectedItems.join(',')
      Proxy(appDelete({ ids: ids }))
    },
    toActive (Proxy, id, status) {
      status === '1' && Proxy(appStop({ ids: id }))
      status !== '2' && Proxy(appStart({ ids: id }))
    },
    // 详情
    btnDetailClick (row, index) {
      this.modalShow = true
      this.modalFlag = 'detail'
      const { id, appName, appType, appDesc } = row
      Object.assign(this.modalForm, { id, appName, appType, appDesc })
      // alert('索引：' + index + ';\n值：' + JSON.stringify(row))
    },
    formModalBeforeClick () {
      if (this.modalFlag === 'detail') {
        this.modalShow = false
      } else {
        return true
      }
    },
    // 提交
    formModalClick (Proxy) {
      switch (this.modalFlag) {
        case 'create':
          this.modalSubmit(Proxy); break
        case 'update':
          this.modalUpdate(Proxy); break
      }
    },
    modalSubmit (Proxy) {
      const { id, ...param } = this.modalForm // 排除id属性进行提交
      Proxy(appAdd(param)).then(response => {
        console.log('=Create=应用新增成功==', response.data)
      })
    },
    modalUpdate (Proxy) {
      Proxy(appUpdate(this.modalForm))
    }
  }
}
